package com.daoeping.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.json.JSONArray;
import com.daoeping.data.Usr_friend;
import com.daoeping.domain.JsonData;
import com.daoeping.util.ConverntJsonData;

public class Usr_friendgroupDAO extends AllDaoSupport{
	public JsonData findUsr_friendgroupList(Usr_friend usr_friend){
		String hql = "select frigroup.name , frigroup.id from Usr_friend fri left join fri.usr_friendgroup frigroup where fri.usr_userByUserId.id='"+usr_friend.getUsr_userByUserId().getId()+"' and fri.usr_userByFriendId.id is null ";
		if(usr_friend.getUsr_friendgroup()!=null){
			hql = hql + " and fri.usr_friendgroup.id != '"+usr_friend.getUsr_friendgroup().getId()+"'";
		}
		hql = hql + "order by fri.id";
		List<Object[]> resultlist = (List<Object[]>)this.selectDataByHQL(hql);
		JSONArray jsonArray = new ConverntJsonData().getJsonData(resultlist);
		JsonData jsonData = new JsonData();
		jsonData.setJsonData(jsonArray+"");
		return jsonData;
	}
	
	public static void main(String[] args) {
		SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sf.openSession();
		String hql = "select frigroup.name , frigroup.id from Usr_friend fri left join fri.usr_friendgroup frigroup where fri.usr_userByUserId.id='1' and fri.usr_userByFriendId.id is null order by fri.id"; 
		//select frigroup.name , frigroup.id from Usr_friend fri left join fri.usr_friendgroup frigroup where fri.usr_userByUserId.id='1' and fri.usr_userByFriendId.id is null order by fri.id
		List<Object[]> list = session.createQuery(hql).list();
		System.out.println(list);
		for (Object[] objects : list) {
			System.out.println(objects[0]);
		}
	}
}
